package features;
import j3d.InvalidBooleanOperationException;
import j3d.J3D;
import java.io.File;
import operationSolids.CSGSolid;
import operationSolids.CompoundSolid;
import operationSolids.OperationBlock;
import CAD.MainFrame;
import bool.*;

public class Block {

	public CSGSolid block = null;


	public float length=0;
	public float width=0;
	public float height=0;
	public float tolerance=0;
	public String name;




	public Block(float width, float height, float length,float tolerance,String name){
		this.height = height;
		this.width = (float)width;
		this.length = (float)length;
		this.tolerance=tolerance;
		this.name=name;
		block = new OperationBlock("block",length,height,width);
		MainFrame.project.setBlock(this);
		//block.translate(width*5/2, heigth*5/2, length*5/2);		
	}

	public Block(String name, int op, Block s1,CSGSolid s2){

		this.height = s1.height;
		this.width = s1.width;
		this.length = s1.length;
		this.tolerance=s1.tolerance;
		this.name=s1.name;
		try {
			this.block = new CompoundSolid(name, op, s1.block, s2);
		} catch (InvalidBooleanOperationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		MainFrame.project.setBlock(this);

	}
	public CSGSolid makeRawBlock() {	
		return block = new OperationBlock("block",length,height,width);
	}

	public void rotateFaceXY(int face){

		switch (MainFrame.Face) {
		case MainFrame.TOPFACE:
			switch (face) {

			case MainFrame.BACKFACE:
				block.rotate(Math.PI/2,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.EASTFACE:
				block.rotate(0,-Math.PI/2);//ok1
				block.rotate(-Math.PI/2,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.LOWERFACE:
				block.rotate(-Math.PI,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.WESTFACE:
				block.rotate(0,Math.PI/2);//ok1
				block.rotate(-Math.PI/2,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.FRONTFACE:
				block.rotate(-Math.PI/2,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			default:
				System.out.print("ja esta na face certa! (TOPFACE)");
			break;
			}
			break;

		case MainFrame.BACKFACE:
			switch (face) {

			case MainFrame.TOPFACE:
				block.rotate(-Math.PI/2,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.EASTFACE:
				block.rotate(-Math.PI/2,0);//ok1
				block.rotate(0,-Math.PI/2);//ok1
				block.rotate(-Math.PI/2,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.LOWERFACE:
				block.rotate(-Math.PI/2,0);//ok
				block.rotate(-Math.PI,0);//ok
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.WESTFACE:
				block.rotate(-Math.PI/2,0);//ok
				block.rotate(0,Math.PI/2);//ok1
				block.rotate(-Math.PI/2,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.FRONTFACE:
				block.rotate(-Math.PI/2,0);//ok
				block.rotate(-Math.PI/2,0);//ok
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			default:
				System.out.print("ja esta na face certa! (BACKFACE)");
			break;
			}

			break;
		case MainFrame.EASTFACE:
			switch (face) {

			case MainFrame.TOPFACE:
				block.rotate(Math.PI/2,0);//ok1
				block.rotate(0,Math.PI/2);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.BACKFACE:
				block.rotate(Math.PI/2,0);//ok1
				block.rotate(0,Math.PI/2);//ok1
				block.rotate(Math.PI/2,0);
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.LOWERFACE:
				block.rotate(Math.PI/2,0);//ok1
				block.rotate(0,Math.PI/2);//ok1
				block.rotate(-Math.PI,0);
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.WESTFACE:
				block.rotate(Math.PI/2,0);//ok1
				block.rotate(0,Math.PI/2);//ok1
				block.rotate(0,Math.PI/2);//ok1
				block.rotate(-Math.PI/2,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.FRONTFACE:
				block.rotate(Math.PI/2,0);//ok1
				block.rotate(0,Math.PI/2);//ok1
				block.rotate(-Math.PI/2,0);
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			default:
				System.out.print("ja esta na face certa! (EASTFACE)");
			break;
			}

			break;
		case MainFrame.LOWERFACE:
			switch (face) {

			case MainFrame.TOPFACE:
				block.rotate(Math.PI,0);
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.BACKFACE:
				block.rotate(Math.PI,0);
				block.rotate(Math.PI/2,0);
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.EASTFACE:
				block.rotate(Math.PI,0);
				block.rotate(0,-Math.PI/2);//ok1
				block.rotate(-Math.PI/2,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.WESTFACE:
				block.rotate(Math.PI,0);
				block.rotate(0,Math.PI/2);//ok1
				block.rotate(-Math.PI/2,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.FRONTFACE:
				block.rotate(Math.PI,0);
				block.rotate(-Math.PI/2,0);
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			default:
				System.out.print("ja esta na face certa! (LOWERFACE)");
			break;
			}

			break;
		case MainFrame.WESTFACE:
			switch (face) {

			case MainFrame.TOPFACE:
				block.rotate(Math.PI/2,0);//ok1
				block.rotate(0,-Math.PI/2);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.BACKFACE:
				block.rotate(Math.PI/2,0);//ok1
				block.rotate(0,-Math.PI/2);//ok1
				block.rotate(Math.PI/2,0);
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.EASTFACE:
				block.rotate(Math.PI/2,0);//ok1
				block.rotate(0,-Math.PI/2);//ok1
				block.rotate(0,-Math.PI/2);//ok1
				block.rotate(-Math.PI/2,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.LOWERFACE:
				block.rotate(Math.PI/2,0);//ok1
				block.rotate(0,-Math.PI/2);//ok1
				block.rotate(-Math.PI,0);
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.FRONTFACE:
				block.rotate(Math.PI/2,0);//ok1
				block.rotate(0,-Math.PI/2);//ok1
				block.rotate(-Math.PI/2,0);
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			default:
				System.out.print("ja esta na face certa! (WESTFACE)");
			break;
			}

			break;
		case MainFrame.FRONTFACE:
			//<front>
			switch (face) {

			case MainFrame.TOPFACE:
				block.rotate(Math.PI/2,0);
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;	

			case MainFrame.BACKFACE:
				block.rotate(Math.PI/2,0);
				block.rotate(Math.PI/2,0);
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.EASTFACE:
				block.rotate(Math.PI/2,0);
				block.rotate(0,-Math.PI/2);//ok1
				block.rotate(-Math.PI/2,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.LOWERFACE:
				block.rotate(Math.PI/2,0);
				block.rotate(-Math.PI,0);
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			case MainFrame.WESTFACE:
				block.rotate(Math.PI/2,0);
				block.rotate(0,Math.PI/2);//ok1
				block.rotate(-Math.PI/2,0);//ok1
				MainFrame.Face=face;		
				MainFrame.j3d.removeSolid();
				MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);
				break;

			default:
				System.out.print("ja esta na face certa! (FRONTFACE)");
			break;
			}

			break;
			//</front>
		default:
			System.out.print("erro");
		break;
		}


		MainFrame.Face=face;		
		MainFrame.j3d.removeSolid();
		MainFrame.getJ3d().addSolid((new Block ("comp",CompoundSolid.UNION,MainFrame.block,MainFrame.block.block)).block);

	}







}
